package com.ilop.sthome.ui.fragment.scene;

import android.os.Bundle;
import android.util.Log;

import com.example.common.base.BasePresentFragment;
import com.example.common.utils.LiveDataBus;
import com.example.common.view.refresh.CustomRefreshView;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.event.EventAnswerOK;
import com.ilop.sthome.data.event.EventRefreshScene;
import com.ilop.sthome.data.event.EventSynchronization;
import com.ilop.sthome.data.greenDao.SceneBean;
import com.ilop.sthome.mvp.contract.scene.SceneChangeContract;
import com.ilop.sthome.mvp.present.scene.SceneChangePresenter;
import com.ilop.sthome.ui.activity.scene.AddSceneActivity;
import com.ilop.sthome.ui.adapter.scene.SceneChildAdapter;
import com.ilop.sthome.utils.CoderUtils;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.FragmentSceneChildBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2019-12-30.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：场景页面
 */
public class SceneChildFragment extends BasePresentFragment<SceneChangePresenter, FragmentSceneChildBinding> implements SceneChangeContract.IView {

    private String mDeviceName;
    private SceneChildAdapter mAdapter;
    private int mDeleteSceneId = -1;

    public static SceneChildFragment newInstance(String deviceName) {
        Bundle args = new Bundle();
        args.putString(CommonId.KEY_DEVICE_NAME, deviceName);
        SceneChildFragment fragment = new SceneChildFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_scene_child;
    }

    @Override
    protected void initialize() {
        super.initialize();
        if (getArguments()!=null){
            mDeviceName = getArguments().getString(CommonId.KEY_DEVICE_NAME);
        }
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresenter = new SceneChangePresenter(mContext, mDeviceName);
    }

    @Override
    protected void initView() {
        super.initView();
        mAdapter = new SceneChildAdapter(mContext);
        mDBind.sceneChildRecycle.setAdapter(mAdapter);
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.KEY_MODIFY_SCENE, Integer.class).observe(this, integer -> mPresenter.getSceneList());

        LiveDataBus.get().with(CommonId.SCENE_ITEM_CLICK, SceneBean.class).observe(this, sceneBean -> {
            if (sceneBean!=null) {
                Log.i(TAG, "initDataBus: item click" + mDeviceName + "--" + sceneBean.getDeviceName());
                if (mDeviceName.equals(sceneBean.getDeviceName())) {
                    mPresenter.changeScene(sceneBean);
                }
            }
        });

        LiveDataBus.get().with(CommonId.SCENE_ITEM_LONG_CLICK, SceneBean.class).observe(this, sceneBean -> {
            if (sceneBean!=null) {
                Log.i(TAG, "initDataBus: item long click" + mDeviceName + "--" + sceneBean.getDeviceName());
                mPresenter.showDialogToDelete(sceneBean);
            }
        });

        LiveDataBus.get().with(CommonId.KEY_EVENT_REFRESH_SCENE, EventRefreshScene.class).observe(this, event -> {
            if (event != null){
                Log.i(TAG, "initDataBus: refresh scene");
                if (event.getDeviceName().equals(mDeviceName)) {
                    mPresenter.getSceneList();
                }
            }
        });

        LiveDataBus.get().with(CommonId.KEY_EVENT_SYNCHRONIZATION, EventSynchronization.class).observe(this, event -> {
            if (event!=null){
                Log.i(TAG, "onEventMainThread: synchronization" + event.getState());
                if (event.getState() == EventSynchronization.SYN_AUTO) {
                    if (event.getDeviceName().equals(mDeviceName)) {
                        mPresenter.getSceneList();
                        mPresenter.synchronizationAuto(event.getDeviceName());
                    }
                }
            }
        });

        LiveDataBus.get().with(CommonId.KEY_ANSWER_OK, EventAnswerOK.class).observe(this, event -> {
            if (event!=null) {
                int result = CoderUtils.getAnswerResult(event.getData_str1(), event.getData_str2());
                Log.i(TAG, "initDataBus: answerOK--" + result);
                if (result == 6) {
                    mPresenter.saveCurrentState();
                } else if (result == 39) {
                    dismissProgressDialog();
                    if (mDeleteSceneId > -1) mPresenter.deleteSceneSuccess(mDeleteSceneId);
                } else if (result == 0) {
                    showToast(getString(R.string.failed));
                }
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.sceneChildRecycle.setOnEmptyListener(() -> skipAnotherActivity(AddSceneActivity.class));
        mDBind.sceneChildRecycle.setOnLoadListener(new CustomRefreshView.OnLoadListener() {
            @Override
            public void onRefresh() {
                mPresenter.getGatewayList();
                mPresenter.synchronizationScene(mDeviceName);
                mDBind.sceneChildRecycle.complete();
            }

            @Override
            public void onLoadMore() {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getSceneList();
    }

    @Override
    public void showSceneList(List<SceneBean> scene) {
        mAdapter.setList(scene);
    }

    @Override
    public void showToastMsg(String msg) {
        showToast(msg);
    }

    @Override
    public void showProgress() {
        showProgressDialog();
    }

    @Override
    public void setDeleteSceneId(int sceneId) {
        this.mDeleteSceneId = sceneId;
    }
}
